﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DataTransferObject.Location;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Location.Interface;
using WMS.FC.Domain.Service.System;

namespace WMS.FC.Domain.Service.Location
{
    public class AddLocationService : ServiceBase, IAddLocationService
    {
        protected readonly ILocationRepository locationRepository;

        public AddLocationService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            ILocationRepository locationRepository)
            : base(logger, options, mapper)
        {
            this.locationRepository = locationRepository;
        }

        public async Task<ActionResultBase> AddLocation(AddLocationDTO dTO, UserInfo userInfo)
        {
            var result = new ActionResultBase("添加货位");
            try
            {
                if (string.IsNullOrWhiteSpace(dTO.LineCode))
                    return result.ArgumentError("产线编码不能为空");

                if (string.IsNullOrWhiteSpace(dTO.ZoneCode))
                    return result.ArgumentError("区域编码不能为空");

                if (string.IsNullOrWhiteSpace(dTO.EquipmentCode))
                    return result.ArgumentError("堆垛机编码不能为空");

                if (string.IsNullOrWhiteSpace(dTO.TypeCode))
                    return result.ArgumentError("类型编码不能为空");

                if (string.IsNullOrWhiteSpace(dTO.StateCode))
                    return result.ArgumentError("状态编码不能为空");

                if (string.IsNullOrWhiteSpace(dTO.UseStateCode))
                    return result.ArgumentError("使用状态编码不能为空");

                if (dTO.StartLine <= 0 || dTO.EndLine <= 0 || dTO.EndLine < dTO.StartLine)
                    return result.ArgumentError("起始行和终止行不正确");

                if (dTO.StartColumn <= 0 || dTO.EndColumn <= 0 || dTO.EndColumn < dTO.StartColumn)
                    return result.ArgumentError("起始列和终止列不正确");

                if (dTO.StartLayer <= 0 || dTO.EndLayer <= 0 || dTO.EndLayer < dTO.StartLayer)
                    return result.ArgumentError("起始层和终止层不正确");

                if (dTO.StartDepth <= 0 || dTO.EndDepth <= 0 || dTO.EndDepth < dTO.StartDepth)
                    return result.ArgumentError("起始深度和终止深度不正确");

                var count = 0;
                for (var line = dTO.StartLine; line <= dTO.EndLine; line++)
                {
                    for (var column = dTO.StartColumn; column <= dTO.EndColumn; column++)
                    {
                        for (var layer = dTO.StartLayer; layer <= dTO.EndLayer; layer++)
                        {
                            for (var depth = dTO.StartDepth; depth <= dTO.EndDepth; depth++)
                            {
                                var locationPO = new LocationPO()
                                {
                                    WarehouseCode = options.Value.WarehouseCode,
                                    Code = $"{dTO.ZoneCode}{line:000}{column:000}{layer:000}{depth}",
                                    LineCode = dTO.LineCode,
                                    ZoneCode = dTO.ZoneCode,
                                    EquipmentCode = dTO.EquipmentCode,
                                    TypeCode = dTO.TypeCode,
                                    StateCode = dTO.StateCode,
                                    UseStateCode = dTO.UseStateCode,
                                    Line = line,
                                    Column = column,
                                    Layer = layer,
                                    Depth = depth,
                                    Length = 1,
                                    Width = 1,
                                    Height = 1,
                                };

                                count += await locationRepository.Insert(locationPO, userInfo);
                            }
                        }
                    }
                }

                result.Message = $"添加{count}个货位";
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
